<?php
/**
 * 团体单位管理
 *
 * User: kaikaic
 * Date: 2018/10/25
 * Time: 18:01
 */

namespace app\exam\controller;

use app\exam\service\EnterpriseService;
use controller\BasicAdmin;
use service\ConfigService;
use service\ToolsService;
use think\Db;

class Enterprise extends BasicAdmin {

    protected $table = "ExamEnterprise";

    public function index() {
        $this->title = "企业管理";
        $query = array();
        $query['name'] = $this->request->get("name");
        $query['status'] = intval($this->request->get("status"));

        $db = Db::name($this->table);
        if ($query['name']) {
            $db->whereLike("name", "%{$query['name']}%");
        }

        return parent::listRecord($db, "", true, "id desc");
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);
        $cfgStatus = ConfigService::cfgAccountStatus();
        for ($i=0; $i<$size; $i++) {
            $data[$i]['unique_code'] = ToolsService::getEnterpriseUniqueCode($data[$i]);

            $data[$i]['status_txt'] = $cfgStatus[$data[$i]['status']];
        }

        $this->assign('cfg_status', $cfgStatus);
    }

    /**
     * 团体单位-添加
     */
    public function add() {
        if ($this->request->isGet()) {
            $vo = array();
            return view('form', [
                'title'=>'新增团体单位',
                'vo'=>$vo,
            ]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->doInsertAction($data)) {
                $this->showSuccess('添加成功！', '/exam/enterprise/index');
            }else {
                $this->error('添加失败，请稍候再试！');
            }
        }
    }

    /**
     * 团体单位-编辑
     */
    public function edit() {
        if ($this->request->isGet()) {
            $id = $this->request->get('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $enterprise = EnterpriseService::getInstance()->find($id);
            if (empty($enterprise)) {
                $this->error('记录不存在，请重新操作!');
            }

            return view('form', [
                'title'=>'编辑团体单位',
                'vo'=>$enterprise,
            ]);
        }

        if ($this->request->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $data = $this->request->post();
            if ($this->doInsertAction($data)) {
                $this->success('编辑成功！', '');
            }else {
                $this->error('编辑失败，请稍候再试！');
            }
        }
    }

    /**
     * 团体单位-删除
     */
    public function remove() {
        $id = intval($this->request->post('id', ''));
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $enterprise = EnterpriseService::getInstance()->find($id);
        if (empty($enterprise)) {
            $this->error('记录不存在，请重新操作!');
        }

        $result = EnterpriseService::getInstance()->remove(array('id'=>$id));
        if ($result) {
            $this->showSuccess('删除成功!', '/exam/enterprise/index');
        }else {
            $this->error('删除失败，请重新再试!');
        }
    }

    /**
     * 数据写入
     */
    protected function doInsertAction($data) {
        // 企业缩写
        $data['abbr'] = ToolsService::getStringSpell($data['name']);

        if (empty($data['id'])) {
            $data['sort'] = EnterpriseService::getInstance()->getMaxSort() + 1;
            $data['status'] = 1;
            $data['create_time'] = time();

            $result = EnterpriseService::getInstance()->insert($data);
        } else {
            $id = intval($data['id']);
            $result = EnterpriseService::getInstance()->update($data, $id);
        }

        return $result;
    }
}